package com.zjsd.sqeybusiness.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.zjsd.sqeybusiness.domain.Hyinfo;
import com.zjsd.sqeybusiness.domain.Payorderinfo;
import com.zjsd.sqeybusiness.mapper.HydjinfoMapper;
import com.zjsd.sqeybusiness.mapper.HyinfoMapper;
import com.zjsd.sqeybusiness.service.PayBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.RechargeinfoMapper;
import com.zjsd.sqeybusiness.domain.Rechargeinfo;
import com.zjsd.sqeybusiness.service.IRechargeinfoService;

/**
 * 会员充值信息Service业务层处理
 * 
 * @author Zheng
 * @date 2025-03-10
 */
@Service
public class RechargeinfoServiceImpl implements IRechargeinfoService 
{
    @Autowired
    private RechargeinfoMapper rechargeinfoMapper;
    @Autowired
    private HyinfoMapper hyinfoMapper;
    @Autowired
    private PayBaseService payBaseService;
    @Autowired
    private HydjinfoMapper hydjinfoMapper;
    /**
     * 查询会员充值信息
     * 
     * @param rechargeid 会员充值信息ID
     * @return 会员充值信息
     */
    @Override
    public Rechargeinfo selectRechargeinfoById(Long rechargeid)
    {
        return rechargeinfoMapper.selectRechargeinfoById(rechargeid);
    }

    /**
     * 查询会员充值信息列表
     * 
     * @param rechargeinfo 会员充值信息
     * @return 会员充值信息
     */
    @Override
    public List<Rechargeinfo> selectRechargeinfoList(Rechargeinfo rechargeinfo)
    {
        if(rechargeinfo.getEndDate() != null){
            Date endDate = rechargeinfo.getEndDate() ;
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endDate);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            rechargeinfo.setEndDate(calendar.getTime());
        }
        return rechargeinfoMapper.selectRechargeinfoList(rechargeinfo);
    }

    /**
     * 新增会员充值信息
     * 
     * @param rechargeinfo 会员充值信息
     * @return 结果
     */
    @Override
    public int insertRechargeinfo(Rechargeinfo rechargeinfo)
    {
        return rechargeinfoMapper.insertRechargeinfo(rechargeinfo);
    }

    /**
     * 修改会员充值信息
     * 
     * @param rechargeinfo 会员充值信息
     * @return 结果
     */
    @Override
    public int updateRechargeinfo(Rechargeinfo rechargeinfo)
    {
        return rechargeinfoMapper.updateRechargeinfo(rechargeinfo);
    }

    /**
     * 批量删除会员充值信息
     * 
     * @param rechargeids 需要删除的会员充值信息ID
     * @return 结果
     */
    @Override
    public int deleteRechargeinfoByIds(Long[] rechargeids)
    {
        return rechargeinfoMapper.deleteRechargeinfoByIds(rechargeids);
    }

    /**
     * 删除会员充值信息信息
     * 
     * @param rechargeid 会员充值信息ID
     * @return 结果
     */
    @Override
    public int deleteRechargeinfoById(Long rechargeid)
    {
        return rechargeinfoMapper.deleteRechargeinfoById(rechargeid);
    }

    @Override
    public Rechargeinfo saveRechargeinfo(Rechargeinfo rechargeinfo) {
        if(rechargeinfo.getHyid() != null ){
            Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(rechargeinfo.getHyid());
            if(hyinfo != null){
                rechargeinfo.setHyname(hyinfo.getName());
                rechargeinfo.setHyphone(hyinfo.getPhone());
                rechargeinfo.setOpenid(hyinfo.getOpenid());
                rechargeinfo.setWxname(hyinfo.getWxname());
                rechargeinfo.setXmmc("会员年费充值");
                rechargeinfo.setCzsj(new Date());
            }
        }
        rechargeinfo.setZfzt("0");
        rechargeinfoMapper.insertRechargeinfo(rechargeinfo);
        String ip = "";
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            ip =  inetAddress.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("ip:"+ip);
        System.out.println("openid:"+rechargeinfo.getOpenid());
        System.out.println("czje:"+rechargeinfo.getCzje());
        Payorderinfo payorderinfo = payBaseService.createOrder("2", rechargeinfo.getRechargeid(), rechargeinfo.getCzje().toString(),rechargeinfo.getHyid(),"会员充值",rechargeinfo.getOpenid(),ip);
        if(payorderinfo != null){
            rechargeinfo.setContenturl(payorderinfo.getPayinfo());
        }
        return rechargeinfo;
    }

    @Override
    public Rechargeinfo saveRecharge(Rechargeinfo rechargeinfo) {
        if(rechargeinfo.getHyid() != null ){
            Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(rechargeinfo.getHyid());
            if(hyinfo != null){
                rechargeinfo.setHyname(hyinfo.getName());
                rechargeinfo.setHyphone(hyinfo.getPhone());
                rechargeinfo.setOpenid(hyinfo.getOpenid());
                rechargeinfo.setWxname(hyinfo.getWxname());
                rechargeinfo.setXmmc("会员充值");
                rechargeinfo.setCzsj(new Date());
            }
        }
        rechargeinfo.setZfzt("0");
        System.out.println("赠送金额:"+rechargeinfo.getZsje());
        System.out.println("到账金额:"+rechargeinfo.getDzje());
        rechargeinfoMapper.insertRechargeinfo(rechargeinfo);
        String ip = "";
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            ip =  inetAddress.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        Payorderinfo payorderinfo = payBaseService.createOrder("3", rechargeinfo.getRechargeid(), rechargeinfo.getCzje().toString(),rechargeinfo.getHyid(),"会员充值",rechargeinfo.getOpenid(),ip);
        if(payorderinfo != null){
            rechargeinfo.setContenturl(payorderinfo.getPayinfo());
        }
        return rechargeinfo;
    }
}
